package com.project.dao.impl;

import com.project.bean.StudentBean;
import com.project.dao.IStudentDao;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends BaseDao implements IStudentDao {
    @Override
    public void add(StudentBean student) {
        this.updateData("INSERT INTO t_student(studentName,phone,birthday,grade) values(?,?,?,?)",
                student.getStudentName(),student.getPhone(),student.getBirthday(),student.getGrade());
    }

    @Override
    public void del(Integer id) {
        this.updateData("delete from t_student where id=?",id);
    }

    @Override
    public void update(Integer id, String phone) {
        this.updateData("update t_student set phone=? where id=?",phone,id);
    }

    @Override
    public StudentBean findById(Integer id) {
        List<StudentBean> list = this.find("select * from t_student where id=?",
                StudentBean.class,id);
        if(list.size() != 0){
            return list.get(0);
        }

        return null;
    }

    @Override
    public List<StudentBean> findAll() {
        return this.find("select id,studentName from t_student",StudentBean.class);
    }

    @Override
    public List<StudentBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        String sql = "select * from t_student where 1=1 ";
        List valueList = new ArrayList();
        if(name != null && name.length() != 0){
            sql += "and studentName like  ? ";
            valueList.add("%"+name+"%");
        }
        if(startDate != null){
            sql += "and birthday >= ? ";
            valueList.add(startDate);
        }
        if(endDate != null){
            sql += "and birthday <= ? ";
            valueList.add(endDate);
        }

        return this.find(sql,
                StudentBean.class,valueList.toArray());
    }


}
